<template>
  <div class="login-container">
    <div class="login-top">
      <div class="invite-meg" v-if="merchanismName.length > 0">
        <p class="invite-name">{{merchanismName}}</p>邀请您加入爱联商圈！
      </div>
      <div class="login-to" @click="queryCheckStatus">查询审核状态</div>
      <div class="login-to" @click="goLogin">登陆机构后台管理</div>
      <el-dialog :visible.sync="dialogCheck" :modal-append-to-body='false'>
        <el-card>
          <el-input clearable class="filter-input" style="width: 300px" placeholder="请输入添加机构时的手机号" v-model="checkTelephone">
          </el-input>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getCheckStatus">查找</el-button>
        </el-card>
        <el-card style="margin-top:30px;">
          <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row @selection-change="selsChange">
            <el-table-column align="center" min-width="150px" label="机构名称" prop="name">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="审核状态" prop="checkStatus" :formatter="formatRoleType">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="描述" prop="checkWord">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="机构等级" prop="level">
              <template slot-scope="scope">
                <el-tag>{{LList[scope.row.level]}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="reCheck(scope.row)" v-if="scope.row.checkStatus == 3">重新审核</el-button>
                <el-button type="primary" size="small" @click="noCheck(scope.row)" v-if="scope.row.checkStatus == 3">申请退款</el-button>
                <el-tag type="danger" size="small" v-if="scope.row.checkStatus == 3">未通过</el-tag>
                <el-tag type="primary" size="small" v-if="scope.row.checkStatus == 2">已通过</el-tag>
                <el-tag type="primary" size="small" v-if="scope.row.checkStatus == 1">审核中</el-tag>
                <el-tag type="primary" size="small" v-if="scope.row.checkStatus == 4">退款中</el-tag>
                <el-tag type="primary" size="small" v-if="scope.row.checkStatus == 5">已退款</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-dialog>
      <el-dialog class="loginForm" :visible.sync="dialogFormVisible" :modal-append-to-body='false'>
        <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
          <div class="title-container">
            <h3 class="title">机构管理系统</h3>
          </div>
          <el-form-item prop="username">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="user" />
            </span>
            <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="用户名" />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input name="password" :type="passwordType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" placeholder="密码" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon icon-class="eye" />
            </span>
          </el-form-item>

          <el-button type="primary" style="width:48%;margin-bottom:10px;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
          <el-button type="primary" style="width:48%;margin-bottom:10px;" :loading="loading" @click.native.prevent="resetShow">忘记密码</el-button>
          <div class="tips">
          </div>
        </el-form>
      </el-dialog>

      <el-dialog class="loginForm" :visible.sync="resetFormVisible" :modal-append-to-body='false'>
        <el-form autoComplete="on" :model="resetForm" :rules="resetRules" ref="resetForm" label-position="left">
          <div class="title-container">
            <h3 class="title">重置密码</h3>
          </div>
          <el-form-item prop="mobile">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="user" />
            </span>
            <el-input name="mobile" type="text" v-model="resetForm.mobile" autoComplete="on" placeholder="手机号码" />
          </el-form-item>

          <el-form-item prop="securityCode">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="user" />
            </span>
            <el-input name="securityCode" style="width:45%;" type="text" v-model="resetForm.securityCode" autoComplete="on" placeholder="验证码" />
            <el-button type="primary" style="width:45%;" @click.native.prevent="getConfirm" :disabled="binddisabled">{{bindContent}}</el-button>
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input name="password" :type="passwordType" v-model="resetForm.password" autoComplete="on" placeholder="密码" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon icon-class="eye" />
            </span>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input name="confirmPassword" :type="passwordType" v-model="resetForm.confirmPassword" autoComplete="on" placeholder="确认密码" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon icon-class="eye" />
            </span>
          </el-form-item>

          <el-button type="primary" style="width:45%;margin-bottom:10px;display:block;margin:0 auto;" :loading="loading" @click.native.prevent="handleConfirm">确认修改</el-button>
          <div class="tips">
          </div>
        </el-form>
      </el-dialog>
      <!-- <div class="info-meg">
        <img class="info-img" src="https://ss3.bdstatic.com/6LZ1dD3d1sgCo2Kml5_Y_D3/sys/portrait/item/3b8d1d84" />
        <div class="info-name">机构中心</div>
        <div class="info-out">退出</div>
      </div> -->
    </div>
    <div class="login-center" :style="backgroundImage">
      <div v-for="(item) in items" :key="item.id">
        <div class="login-center-type">
          <ul class="type-item">
            <li class="first-item">
              <div class="item-name">
                <div class="name">{{item.name}}</div>
                <img class="item-icon" :src="item.iconUrl">
              </div>
              <div class="item-price">{{item.fee}}
                <span style="font-size:18px;font-weight:400" v-if="item.fee != 19800 && item.fee != 198000">元/年</span>
                <span style="font-size:18px;font-weight:400" v-if="item.fee == 19800">元/3年</span>
                <span style="font-size:18px;font-weight:400" v-if="item.fee == 198000">元/终生</span>
              </div>
              <div class="wi">开通爱联商圈会员，享受优质模式体系</div>
              <div class="english">Ai Lian Business Circle Distinguished Member</div>
              <div class="item-botton" @click="confirmItem(item.id)">开通{{item.name}}</div>
            </li>
            <!-- <li>
              <pre class="item-warn">{{item.item}}
              </pre>
            </li> -->
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listProxyCategory } from '@/api/login'
import { getMechanismName, queryCheckMeg, listMechanisLevel, sendConfirmCode, updatePassword } from '@/api/mechanism'
import { goNoCheck } from '@/api/proxy'
import { getAES, setAES } from '@/utils/index'
export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (validateUsername == null) {
        callback(new Error('请输入正确的管理员用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('管理员密码长度应大于6'))
      } else {
        callback()
      }
    }
    const validateconfirmPassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('管理员密码长度应大于6'))
      } else if (value !== this.resetForm.password) {
        callback(new Error('两次输入的密码不一致！'))
      } else {
        callback()
      }
    }
    var validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入手机号码'))
      } else {
        var reg = /^1[23456789]\d{9}$/
        if (!reg.test(value)) {
          callback(new Error('请输入有效的手机号码'))
        }
      }
      callback()
    }
    return {
      list: null,
      time: 0,
      bindContent: '获取验证码',
      binddisabled: false,
      merchanismName: '',
      merchanismId: '',
      items: '',
      backgroundImage:
        'background-image:url(https://ailianshangquan.oss-cn-beijing.aliyuncs.com/6zn357besas6euwjn8b9.png);background-repeat:no-repeat;background-size:100% 100%',
      loginForm: {
        username: '',
        password: ''
      },
      resetForm: {
        mobile: '',
        securityCode: '',
        password: '',
        confirmPassword: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      resetRules: {
        mobile: [{ required: true, trigger: 'blur', validator: validatePhone }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: validateconfirmPassword }]
      },
      passwordType: 'password',
      loading: false,
      dialogFormVisible: false,
      resetFormVisible: false,
      dialogCheck: false,
      checkTelephone: undefined,
      listLoading: false,
      mLevelList: [],
      LList: {}
    }
  },
  created() {
    this.getList()
    this.init()
    this.getmlevel()
  },
  methods: {
    init() {
      if (this.$route.query.id == null) {
        return
      }
      const goodsId = parseInt(setAES(this.$route.query.id))
      this.merchanismId = goodsId
      getMechanismName({ id: goodsId }).then(res => {
        this.merchanismName = res.data.data
      })
    },
    confirmItem(itemId) {
      if (itemId === 1) {
        this.$confirm('体验用户暂不开放', '提示', {
          confirmButtonText: '确定',
          type: 'warning',
          center: true
        }).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
      }
      if (itemId === 4) {
        this.$confirm('开通事业部请联系爱能即可', '提示', {
          confirmButtonText: '确定',
          type: 'warning',
          center: true
        }).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
      }
      if (itemId === 2 || itemId === 3) {
        const merchanismIds = this.merchanismId
        this.$router.push({ path: '/regist', query: { id: getAES(itemId.toString()), merchanismId: merchanismIds }})
      }
    },
    getList() {
      listProxyCategory().then(res => {
        this.items = res.data.data.items
      })
    },
    selsChange(sels) {
      this.sels = sels
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleConfirm() {
      this.$refs['resetForm'].validate(valid => {
        if (valid) {
          updatePassword(this.resetForm)
            .then(response => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.resetFormVisible = false
              this.dialogFormVisible = true
              this.resetForm()
            })
            .catch(response => {
              this.$message({
                title: '提示',
                message: response.data.errmsg,
                type: 'warning'
              })
            })
        }
      })
    },
    getConfirm() {
      var reg = 11 && /^((12|13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
      var phoneNum = this.resetForm.mobile
      if (!phoneNum) {
        // 未输入手机号
        this.$message({
          message: '提示，请输入手机号',
          type: 'warning'
        })
        return
      }
      if (!reg.test(phoneNum)) {
        // 手机号不合法
        this.$message({
          message: '提示，请输入正确的手机号码！',
          type: 'warning'
        })
      }
      this.time = 60
      this.timer()
      // 发送验证码
      sendConfirmCode({ mobile: this.resetForm.mobile })
        .then(response => {
          if (response.data.errno === 0) {
            // this.getCheckStatus()
            this.$message({
              type: 'success',
              message: '发送成功'
            })
          }
        })
        .catch(response => {
          this.$message({
            title: '提示',
            message: response.data.errmsg,
            type: 'warning'
          })
        })
    },
    timer() {
      if (this.time > 0) {
        this.time--
        this.bindContent = this.time + '秒后重新获取'
        this.binddisabled = true
        setTimeout(() => {
          this.timer()
        }, 1000)
      } else if (this.time === 0) {
        this.btnContent = '获取验证码'
        this.binddisabled = false
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.$store
            .dispatch('LoginByUsername', this.loginForm)
            .then(() => {
              this.$message({
                message: '登录成功',
                type: 'success'
              })
              this.loading = false
              this.$router.push({ path: '/' })
            })
            .catch(errmsg => {
              this.$message.error(errmsg.data.errmsg)
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    resetShow() {
      this.dialogFormVisible = false
      this.resetFormVisible = true
    },
    formatRoleType: function(row, column) {
      return row.checkStatus === 1
        ? '审核中'
        : row.checkStatus === 2
          ? '已通过'
          : row.checkStatus === 3
            ? '未通过'
            : row.checkStatus === 0
              ? '老用户'
              : row.checkStatus === 4 ? '退款中' : row.checkStatus === 3 ? '已退款' : '其他'
    },
    goLogin() {
      // var that = this
      this.dialogFormVisible = true
    },
    getmlevel() {
      listMechanisLevel()
        .then(response => {
          this.mlevel = response.data.data.ListProxy
          this.mLevelList = response.data.data.brandList
          for (let i = 0; i < this.mLevelList.length; i++) {
            this.LList[this.mLevelList[i].value] = this.mLevelList[i].label
          }
        })
        .catch(() => {
          this.mlevel = []
        })
    },
    queryCheckStatus() {
      this.list = []
      this.dialogCheck = true
    },
    getCheckStatus() {
      this.listLoading = true
      var reg = /^1[3456789]\d{9}$/
      if (!reg.test(this.checkTelephone)) {
        this.dialogCheck = false
        this.listLoading = false
        this.$alert('请输入正确的手机号码！', {
          confirmButtonText: '确定',
          type: 'warning'
        })
      } else {
        queryCheckMeg({ telephone: this.checkTelephone })
          .then(response => {
            this.list = response.data.data.items
            this.listLoading = false
          })
          .catch(() => {
            this.list = []
            this.listLoading = false
          })
      }
    },
    reCheck(row) {
      this.$router.push({ path: '/reRegist', query: { merchanismId: row.id }})
    },
    resetForms() {
      this.resetForm = {
        mobile: '',
        securityCode: '',
        password: '',
        confirmPassword: ''
      }
    },
    noCheck(row) {
      this.$confirm('申请退款, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        goNoCheck({ id: getAES(row.id.toString()), recordSn: getAES(row.recordSn.toString()) })
          .then(response => {
            if (response.data.errno === 0) {
              this.getCheckStatus()
              this.$message({
                type: 'success',
                message: '退款成功'
              })
            }
          })
          .catch(response => {
            this.$message({
              title: '提示',
              message: response.data.errmsg,
              type: 'warning'
            })
          })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgb(45, 58, 75);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
ul,
li {
  padding: 0;
  margin: 0;
  list-style: none;
}
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  flex-flow: column nowrap;
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: #2d3a4b;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.loginForm {
  width: 1000px;
  margin: auto;
  z-index: 10001 !important;
}
.login-top {
  width: 100%;
  height: 10%;
  background: #000;
  border-bottom: 1px solid #f9d681;
  cursor: pointer;
}
.login-center {
  width: 100%;
  height: 90%;
  background-size: 180% 100%;
  background-repeat: no-repeat;
}
.login-foot {
  width: 100%;
  height: 10%;
}
.login-img {
  height: 100%;
}
.login-center-type {
  float: left;
  width: 25%;
  height: 100%;
  .type-item {
    width: 90%;
    height: 220px;
    margin-top: 50%;
    margin-left: 5%;
    background: -webkit-linear-gradient(
      left,
      rgba(200, 182, 146, 0.9),
      rgba(187, 156, 109, 0.9)
    ); /* Safari 5.1 - 6.0 */
    background: -o-linear-gradient(right, rgba(200, 182, 146, 0.9), rgba(187, 156, 109, 0.9)); /* Opera 11.1 - 12.0 */
    background: -moz-linear-gradient(right, rgba(200, 182, 146, 0.9), rgba(187, 156, 109, 0.9)); /* Firefox 3.6 - 15 */
    background: linear-gradient(to right, rgba(200, 182, 146, 0.9), rgba(187, 156, 109, 0.9)); /* 标准的语法 */
    // box-shadow:1px 1px 5px white;
    border-radius: 10px;
    .first-item {
      font-size: 20px;
      margin: auto;
      text-align: center;
      padding-top: 30px;
      border-radius: 10px;
      .item-name {
        height: 30px;
        line-height: 30px;
        display: inline-block;
        vertical-align: middle;
        width: 100%;
        // color: #ca963b;
        font-weight: 500;
        font-size: 25px;
      }
      .item-botton {
        cursor: pointer;
        width: 200px;
        height: 40px;
        font-size: 15px;
        line-height: 40px;
        text-align: center;
        border-radius: 23px;
        /* margin-top: 20px; */
        margin: 20px auto;
        margin-top: 20px;
        border: 1px solid #f9d681;
        color: #f9d681;
        margin-top: 10px;
        background-color: #706163;
      }
    }
  }
}
.item-warn {
  white-space: pre-wrap;
  word-wrap: break-word;
  width: 80%;
  margin: auto;
  margin-top: 20px;
  line-height: 25px;
  font-size: 14px;
}
.item-price {
  font-size: 30px;
  font-weight: 500;
  color: black;
  width: 100%;
  text-align: left;
  margin-left: 36px;
  letter-spacing: 2px;
  margin-top: 10px;
  margin-bottom: 4px;
}
.filter-input {
  background-color: #2d3a4b;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 5px;
  margin-right: 30px;
}
.name {
  text-align: left;
  margin-left: 36px;
}
// .login-center-type :hover .first-item {
//   color: #ca963b;
// }
// .login-center-type :hover .item-botton {
//   border: 1px solid #f9d681 !important;
//   background-color: #f9d681;
//   color: #351d06 !important;
// }
// .login-center-type :hover {
//   border-color: #f2ddaf;
//   background-color: #fffaeb;
// }
.login-to {
  float: right;
  margin-right: 50px;
  margin-top: 30px;
  font-size: 12px;
  width: 150px;
  text-align: center;
  height: 40px;
  line-height: 40px;
  border-radius: 20px;
  border: 1px solid #f9d681;
  background-color: rgb(112, 97, 63);
  color: #f9d681;
  font-weight: bold;
}
.english {
  color: rgb(148, 133, 101);
  margin-left: 10px;
  font-size: 18px;
}
.company_c {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: inline-block;
  align: center;
  height: 30px;
  width: 330px;
  margin-left: auto;
}
.info-meg {
  margin-top: 20px;
  float: right;
  width: 300px;
  height: 50px;
}
.info-img {
  float: left;
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
.wi {
  display: none;
  color: rgb(237, 223, 205);
  font-size: 16px;
  margin-right: 85px;
}
.info-out {
  background-color: white;
  width: 70px;
  font-size: 12px;
  text-align: center;
  height: 30px;
  line-height: 30px;
  margin-left: 20px;
  margin-top: 10px;
  float: left;
}
.info-name {
  float: left;
  height: 55px;
  font-size: 20px;
  color: #fff;
  line-height: 55px;
  margin-left: 20px;
  display: inline-block;
  *display: inline;
  *zoom: 1;
  vertical-align: middle;
  max-width: 100px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.invite-meg {
  float: left;
  color: #f9d681;
  margin-top: 10px;
  margin-left: 50px;
}
.invite-name {
  display: inline-block;
  letter-spacing: 5px;
  font-size: 20px;
}
.company_c a {
  color: white;
}
.item-icon {
  float: right;
  width: 50px;
  // height: 50px;
  margin-right: 10px;
  margin-top: -20px;
}
.info-icon {
  float: left;
  width: 30px;
  height: 30px;
  margin-top: 10px;
  margin-left: 10px;
}
</style>
